﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<script>
    var keyValue = $.request("keyValue");
    var siteId = $.request("siteId");
    var spaceId = $.request("spaceId");
    var selectValObj = {
        oldValue: 0,
        newValue: 0
    },
    TotalObj = [];

    function find_checkbox(success, error) {
        var $gridList = this;
        var checkboxs = $gridList.find("input[type='checkbox']:checked"),
            selectedVals = [];
        if (checkboxs.length) {
            checkboxs.each(function (n, ele) { selectedVals.push(ele.value); });
            success && success(selectedVals);
        } else {
            error && error();
        }
    }

    function valid_checkbox() {
        var args = Array.prototype.slice.call(arguments);
        if (args.length == 1) {
            args.push(function () {
                $.modalAlert("请选择相关的订单项", "success");
            });
        }
        find_checkbox.apply(this, args)
    }

    function on_yunprice_change_up(self, index) {
        var newValue = $(self).val();
        if (isNaN(newValue)) {
            $(self).val(parseFloat(newValue) || 0);
        }
        selectValObj.newValue = Number($(self).val());

        $("#totalPrice" + index).text(selectValObj.newValue);
        $("#totalPrice_" + index).val(selectValObj.newValue);
        var total = 0;
        for (var i = 0; i < TotalObj.length; i++) {
            var item = TotalObj[i];
            if (item.index == index) {
                total += Number(item.quantity) * selectValObj.newValue;
            } else {
                total += Number(item.totalPrice);
            }
        }
        $("#totalTotalPrice").text(total);
    }

    function on_yunprice_change_down(self, index) {
        selectValObj.oldValue = Number($(self).val());
    }

    $(function () {
        $.ajax({
            url: "/OrderFlowManage/ClientOrder/GetPriceDetailJson?keyValue=" + keyValue + "&siteId=" + siteId + "&spaceId=" + spaceId,
            data: { keyValue: keyValue, siteId: siteId, spaceId: spaceId },
            dataType: "json",
            async: false,
            success: function (data) {
                var index = 1;
                var $previewTakelookGrid = $('#previewTakelookGrid');
                var html = '',
                    tmp = 0,
                    typ = 0,
                    tq = 0,
                    ttp = 0;
                for (var i in data.rows) {
                    var item = data.rows[i];
                    html += '<tr id="tr' + index + '">';
                    html += '<td>';
                    html += index;
                    html += '<input type="hidden" value="' + item.F_Id + '">';
                    html += '</td>';
                    html += '<td>';
                    html += item.F_SiteName;
                    html += '</td>';
                    html += '<td>';
                    html += item.F_SpaceName;
                    html += '</td>';
                    html += '<td>';
                    html += item.F_FeeTypeName;
                    html += '</td>';
                    html += '<td>';
                    html += item.F_UnitName || "";
                    html += '</td>';
                    html += '<td>';
                    html += item.F_SpaceArea || "";
                    html += '</td>';
                    html += '<td>';
                    html += '<span id="marketPrice' + index + '" name="marketPrice' + index + '">' + (item.F_MarketPrice || '/') +'</span>';
                    html += '</td>';
                    html += '<td class="formValue">';
                    //html += '<input type="text" id="yunPrice' + index + '" name="yunPrice' + index + '" class="yunPrice required isFloatGteZero" onkeyup="on_yunprice_change_up(this, ' + index + ')" onkeydown="on_yunprice_change_down(this, ' + index + ')" value="' + item.F_YunPrice + '">';
                    html += '<span id="yunPrice' + index + '" name="yunPrice' + index + '">' + (item.F_YunPrice || '/') +'</span>';
                    html += '</td>';
                    html += '<td>';
                    html += '<input type="text" id="salePrice' + index + '" name="salePrice' + index + '" class="yunPrice required isFloatGteZero" onkeyup="on_yunprice_change_up(this, ' + index + ')" onkeydown="on_yunprice_change_down(this, ' + index + ')" value="' + item.F_SalePrice + '">';
                    html += '</td>';
                    html += '<td>';
                    html += '<span id="quantity' + index + '" name="quantity' + index +'">' + item.F_Quantity + '</span>';
                    html += '</td>';
                    html += '<td>';
                    html += '<span id="totalPrice' + index + '" name="totalPrice' + index + '">' + item.F_TotalPrice + '</span>';
                    html += '<input type="hidden" id="totalPrice_' + index + '" name="totalPrice_' + index + '" class="totalPrice_" value="' + item.F_TotalPrice + '">';
                    html += '</td>';
                    html += '</tr>';
                    

                    tmp += Number(item.F_MarketPrice);
                    typ += Number(item.F_YunPrice);
                    tq += Number(item.F_Quantity);
                    ttp += Number(item.F_TotalPrice);

                    TotalObj.push({
                        "index": index,
                        "yunPrice": Number(item.F_YunPrice),
                        "quantity": Number(item.F_Quantity),
                        "totalPrice": Number(item.F_TotalPrice)
                    });
                    index++;
                }
                $previewTakelookGrid.html(html);
                $("#totalMarketPrice").text(tmp);
                $("#totalYunPrice").text(typ);
                $("#totalQuantity").text(tq);
                $("#totalTotalPrice").text(ttp);
            }
        });
    });

    function submitForm() {
        if (!$('#form1').formValid()) {
            return false;
        }

        var $previewTakelookGrid = $('#previewTakelookGrid');
        var trs = $previewTakelookGrid.find('tr');

        //valid_checkbox.call($previewTakelookGrid, function (cks) {
            
        //});
        var param = {
            orderId: keyValue,
            siteId: siteId,
            spaceId: spaceId,
            data: []
        };
        trs.each(function (index, element) {
            var f_Id = $(this).find("input[type='hidden']").val();
            var yunPrice = $(this).find("input[name*='yunPrice']").val();
            var salePrice = $(this).find("input[name*='salePrice']").val();
            var totalPrice = $(this).find("input[name*='totalPrice_']").val();

            param.data.push({ f_Id: f_Id, yunPrice: yunPrice, SalePrice: salePrice, totalPrice: totalPrice });
        });

        $.submitForm({
            url: "/OrderFlowManage/ClientOrder/SubmitPriceDetailForm?keyValue=" + keyValue,
            param: param,
            success: function () {
                //$.currentWindow().vm.takkelook.initTakelook();
                //$.currentWindow().$("#btn_takelook").prop("disabled", true);

                $.loading(false);
            }
        });
    }
</script>

<form id="form1">
    <div style="margin:0 20px;">
        <table class="table table-condensed table-bordered">
            <thead>
                <tr>
                    <th width="30">序号</th>
                    <th>场地名称</th>
                    <th>使用空间</th>
                    <th>费用类品</th>
                    <th>单位</th>
                    <th>使用面积(㎡)</th>
                    <th>市场刊例价(元)</th>
                    <th>YUNSPACE底价(元)</th>
                    <th>销售价</th>
                    <th>数量</th>
                    <th>合计价格(元)</th>
                </tr>
            </thead>
            <tbody id="previewTakelookGrid"></tbody>
            <tfoot>
                <tr>
                    <td></td>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th>合计价：</th>
                    <th><span id="totalTotalPrice" name="totalTotalPrice" class="text-danger"></span></th>
                </tr>
            </tfoot>
        </table>
    </div>
</form>


